/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GfxMain extends Activity implements OnClickListener {

	ArrayList<Button> arrMenuButtons = new ArrayList<Button>();;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.setTitle(getString(R.string.app_name) + " v"
				+ getString(R.string.app_version));

		preparePreference();
		CtrlMaintanence.getInstance().updatePreferences();
		setDisplayElements();
	}

	public void onClick(View v) {
		int idButton = ((Button) v).getId();
		switch (idButton) {
		case R.id.menu_tutorial:
			startActivity(new Intent(this, GfxTutorial.class));
			break;
		case R.id.menu_customshot:
			startActivity(new Intent(this, GfxQuickShot.class));
			break;
		case R.id.menu_levelshot:
			startActivity(new Intent(this, GfxMasterShot.class));
			break;
		case R.id.menu_customsettings:
			startActivityForResult(new Intent(this, GfxPreferences.class),idButton);
			break;
		case R.id.menu_options:
			startActivityForResult(new Intent(this, GfxOptions.class), idButton);
			break;
		case R.id.menu_help:
			showDialog(R.id.display_help);
			break;
		case R.id.menu_about:
			showDialog(R.id.display_about);
			break;
		case R.id.menu_exit:
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case R.id.menu_customsettings:
			// TODO why resultCode does not send anything??
			// if(resultCode == Activity.RESULT_OK)
			if (SMBGuesstheNumber.isPreferencesChanged) {
				SMBGuesstheNumber.isGameStarted = false;
				CtrlMaintanence.getInstance().savetheGame();
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case R.id.display_help:
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_help_title)
					.setMessage(R.string.dialog_help_text).setPositiveButton(
							R.string.OK, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();
		case R.id.display_about:
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_about_title)
					.setMessage(R.string.dialog_about_text)
					.setPositiveButton(
							R.string.everythingscool, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							})
					.setNeutralButton(
							R.string.iamafan, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									openWepPage("http://www.facebook.com/pages/Android-Game-Guess-the-Number/90769722319?ref=ts");
								}
							})
					.setNegativeButton(
							R.string.ihavesomething, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									openWepPage("http://code.google.com/p/guessthenumber/issues/entry");
								}
							})
					.create();
		}
		return null;
	}

	private void setDisplayElements() {
		
		arrMenuButtons.clear();
		arrMenuButtons.add((Button) findViewById(R.id.menu_tutorial));
		arrMenuButtons.add((Button) findViewById(R.id.menu_customshot));
		arrMenuButtons.add((Button) findViewById(R.id.menu_levelshot));
		arrMenuButtons.add((Button) findViewById(R.id.menu_customsettings));
		arrMenuButtons.add((Button) findViewById(R.id.menu_options));
		arrMenuButtons.add((Button) findViewById(R.id.menu_help));
		arrMenuButtons.add((Button) findViewById(R.id.menu_about));
		arrMenuButtons.add((Button) findViewById(R.id.menu_exit));
		
		for(Button button: arrMenuButtons){
			button.setOnClickListener(this);
		}
	}

	private void preparePreference() {
		SMBGuesstheNumber.generalSettings = getSharedPreferences(
				CtrlMaintanence.PREF_NAME, 0);
	}
	
	private void openWepPage(String url){
		Uri uri = Uri.parse(url);
		Intent myIntent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(myIntent);
	}
}
